/*
 *  app.h
 *  mini
 *
 *  Created by jin on 1/1/11.
 *  Copyright 2011 cs. All rights reserved.
 *
 */

#ifndef INCLUDED_APP_H
#define INCLUDED_APP_H

#if defined(WIN32)
#include <windows.h>
#endif

#include <dlimpexp.h>

class AppImpBase;

class MINI_API AppBase
{
public:
    AppBase();
    virtual ~AppBase();

    virtual bool OnInit();
    virtual void Run();
    virtual void OnExit();
private:
    AppImpBase* pImp;
};

extern "C" MINI_API void miniSetApp(AppBase* theApp);
extern "C" MINI_API AppBase* miniGetApp();

extern "C" MINI_API int miniEntryWin(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
extern "C" MINI_API int miniEntryConsole(int argc, char **argv);

// Macros

#define DECLARE_APP(appname) extern appname& GetApp();

#if defined (WIN32) && defined(_WINDOWS)
#define IMPLEMENT_MINI_MAIN(appname)                                     \
    extern "C" int WINAPI WinMain(HINSTANCE hInstance,                   \
                                  HINSTANCE hPrevInstance,               \
                                  LPSTR lpCmdLine,                       \
                                  int nCmdShow)                          \
    {                                                                    \
        miniSetApp(new appname);                                         \
        return miniEntryWin(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
    }
#else
#define IMPLEMENT_MINI_MAIN(appname)      \
        int main(int argc, char **argv) { \
            miniSetApp(new appname);      \
            return miniEntryConsole(argc, argv); \
        }
#endif

#define IMPLEMENT_APP(appname)            \
    appname& GetApp(){                    \
        return (appname&)(*miniGetApp()); \
    }                                     \
    IMPLEMENT_MINI_MAIN(appname)

#endif //INCLUDED_APP_H

